package org.blindmandesign;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Nathan Bubna
 * @since javaQuery 1.0
 * @version $Id$
 */
public class jQ extends AbstractList {

    private Config config;
    private Parser parser;
    private List<Node> nodes;

    public jQ(String definition) throws Exception {
        this(definition, new SelectorParser());
    }

    public jQ(String definition, Parser parser) throws Exception {
        this(parser.parse(definition), parser);
    }

    public jQ(List<Node> nodes, Parser parser) {
        this.nodes = nodes;
        this.parser = parser;
        this.config = parser.getConfig();
    }

    public Node get(int index) {
        return this.nodes.get(index);
    }

    public int size() {
        return this.nodes.size();
    }

    public static class Act<T extends Object> {
        public T onTag(Tag tag) {
            return null;
        }

        public T onText(Text text) {
            return null;
        }

        public T on(Node n) {
            switch(n.getType()) {
                case TAG:
                    return onTag((Tag)n);
                case TEXT:
                    return onText((Text)n);
                default:
                    return null;
            }
        }
    }

    public jQ each(Act act) {
        for (Node n : nodes) {
            Object result = act.on(n);
            if (Boolean.FALSE.equals(result)) {
                break;
            }
        }
        return this;
    }

    public jQ map(Act<Node> act) {
        return map(act, false);
    }

    public jQ map(Act<Node> act, boolean breakOnNull) {
        List<Node> results = new ArrayList<Node>();
        for (Node n : nodes) {
            Node result = act.on(n);
            if (result == null && breakOnNull) {
                break;
            }
            results.add(result);
        }
        return new jQ(results, parser);
    }

    public List from(Act act) {
        List results = new ArrayList();
        for (Node n : nodes) {
            Object result = act.on(n);
            if (Boolean.FALSE.equals(result)) {
                break;
            }
            if (result != null) {
                results.add(result);
            }
        }
        return results;
    }


    public jQ addClass(Object c) {
        return each(new AddClass(c));
    }
    public static final class AddClass extends Act {
        private Object cls;

        public AddClass(Object cls) {
            this.cls = cls;
        }

        @Override
        public Object onTag(Tag tag) {
            return tag.addClass(cls);
        }
    }


    public jQ attr(Object k, Object v) {
        return each(new SetAttr(k, v));
    }
    public static final class SetAttr extends Act {
        private Object k;
        private Object v;

        public SetAttr(Object k, Object v) {
            this.k = k;
            this.v = v;
        }

        @Override
        public Object onTag(Tag tag) {
            return tag.attr(k, v);
        }
    }


    public String text() {
        StringBuilder out = new StringBuilder();
        each(new GetText(out));
        return out.toString();
    }
    public static final class GetText extends Act<Node> {
        private StringBuilder out;

        public GetText(StringBuilder out) {
            this.out = out;
        }

        @Override
        public Node onTag(Tag tag) {
            return tag.text(out);
        }

        @Override
        public Node onText(Text text) {
            text.render(out);
            return text;
        }
    }

    public jQ text(Object o) {
        return each(new SetText(o));
    }
    public static final class SetText extends Act {
        private Object text;

        public SetText(Object o) {
            this.text = text;
        }

        @Override
        public Object onTag(Tag tag) {
            return tag.text(text);
        }
    }


    public static final Root ROOT = new Root();
    public jQ root() {
        return map(ROOT);
    }
    public static final class Root extends Act<Node> {
        @Override
        public Node on(Node node) {
            return node.root();
        }
    }


    public static final Parent PARENT = new Parent();
    public jQ parent() {
        return map(PARENT);
    }
    public static final class Parent extends Act<Node> {
        @Override
        public Node on(Node node) {
            return node.parent();
        }
    }


    public String toString() {
        StringBuilder s = new StringBuilder();
        each(new Render(s, config));
        return s.toString();
    }
    public static final class Render extends Act {
        private StringBuilder out;
        private Config config;

        public Render(StringBuilder out, Config config) {
            this.out = out;
            this.config = config;
        }

        public Object on(Node node) {
            node.render(out, config);
            return true;
        }
    }

}
